<template>
  <div>
    <!-- 标题 -->
    <template slot="header">已缴费人员名单</template>

    <!-- 新增按钮 -->
    <el-row class="add-btn">
      <el-col :span="4">
        <el-button type="primary" @click="centerDialogVisible = true">新增</el-button>
      </el-col>
      <el-col :span="24">
        <el-input type="text" v-model="searchPerson" placeholder="请输入姓名 / 手机号" style="width:400px;"></el-input>
        <el-button type="primary" @click="searchBtn()" class="search-btn">搜索</el-button>
      </el-col>
    </el-row>

    <!-- 表格列表 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="registration_date" label="报名日期" align="center"></el-table-column>
      <el-table-column prop="school.area.area_name" label="赛区" align="center" width="100px"></el-table-column>
      <el-table-column prop="school.school_name" label="补给校区" align="center" width="100px"></el-table-column>
      <el-table-column prop="student_name" label="学员姓名" align="center" width="100px"></el-table-column>
      <el-table-column prop="ID_number" label="身份证号" align="center" width="160px"></el-table-column>
      <el-table-column prop="birth_date" label="生日" align="center"></el-table-column>
      <el-table-column prop="contact_name" label="联系人" align="center" width="80px"></el-table-column>
      <el-table-column prop="contact_phone" label="联系电话" align="center"></el-table-column>

      <el-table-column prop="gender.value" label="性别" align="center" width="80px"></el-table-column>
      <el-table-column prop="payment_status.value" label="缴费状态" align="center"></el-table-column>
      <el-table-column prop="payment_amount" label="费用" align="center"></el-table-column>

      <el-table-column label="操作" align="center" width="200px">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="editClick(scope.row); centerDialogVisible1 = true"
            size="small"
            class="edit-btn"
          >编辑</el-button>
          <el-button @click="handleClick(scope.row)" type="warning" size="small">缴费</el-button>
          <el-button @click="deleteClick(scope.row)" type="danger" size="small">删除</el-button>
          <!-- <el-button type="text" size="small">编辑</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增弹框 -->
    <el-dialog
      title="新增已缴费"
      :visible.sync="centerDialogVisible"
      width="35%"
      center
      style=" padding:10px 20px 10px 20px"
    >
      <div class="form-list">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="报名日期" class="time">
            <el-date-picker
              v-model="form.create_time"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>

          <el-form-item label="参赛姓名">
            <el-input v-model="form.student_name"></el-input>
          </el-form-item>

          <el-form-item label="身份证号" :rules="rules.IdNum" prop="ID_number">
            <el-input v-model="form.ID_number"></el-input>
          </el-form-item>

          <el-form-item label="生日" prop="birth_date" :rules="rules.BirthNum">
            <el-date-picker
              v-model="form.birth_date"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>

          <el-form-item label="性别">
            <el-select v-model="form.gender" style="display:block">
              <el-option
                v-for="item in form.gender_choice"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="联系人">
            <el-input v-model="form.contact_name"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" :rules="rules.phone" prop="contact_phone">
            <el-input v-model="form.contact_phone"></el-input>
          </el-form-item>

          <el-form-item label="校区">
            <el-input type="text" v-model="form.default_school.name" disabled></el-input>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addStuSubmit('form');">提交</el-button>
      </span>
    </el-dialog>

    <!-- 编辑弹框 -->
    <el-dialog title="编辑信息" :visible.sync="centerDialogVisible1" width="35%" center>
      <div class="form-list">
        <el-form ref="form2" :model="form2" :rules="rules" label-width="80px">
          <el-form-item label="参赛姓名">
            <el-input v-model="form2.student_name"></el-input>
          </el-form-item>

          <el-form-item label="身份证号" :rules="rules.IdNum" prop="ID_number">
            <el-input v-model="form2.ID_number"></el-input>
          </el-form-item>

          <el-form-item label="生日">
            <el-input v-model="form2.birth_date"></el-input>
          </el-form-item>

          <el-form-item label="性别">
            <el-select v-model="form2.gender" style="display:block">
              <el-option
                v-for="item in form2.gender_choice"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="联系人">
            <el-input v-model="form2.contact_name"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" :rules="rules.phone" prop="contact_phone">
            <el-input v-model="form2.contact_phone"></el-input>
          </el-form-item>

          <el-form-item label="缴费状态">
            <el-input v-model="form2.payment_status.value" disabled></el-input>
          </el-form-item>

          <el-form-item label="费用">
            <el-input v-model="form2.payment_amount" disabled></el-input>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit('form2')">提交</el-button>
      </span>
    </el-dialog>

    <!-- 缴费弹窗 -->
    <el-dialog title="缴费金额" :visible.sync="payStatus" width="35%" center>
      <div class="form-list">
        <el-form ref="payList" :model="payList" :rules="rules" label-width="80px">
          <el-form-item label="缴费金额">
            <el-input v-model="payList.pay"></el-input>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="payStatus = false">取 消</el-button>
        <el-button type="primary" @click="paySubmit">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import rules from "../assets/js/rules/rules";
export default {
  name: "unpay",
  data() {
    return {
      // 表单验证
      rules,

      // 新增弹框默认状态
      centerDialogVisible: false,
      centerDialogVisible1: false,
      payStatus: false,

      // 表格列表数据
      tableData: [],
      searchPerson: "",
      payList: {
        pay: "",
        id: ""
      },
      // 新增列表信息
      form: {
        student_name: "",
        ID_number: "",
        birth_date: "",
        registration_date: [],
        payment_status: "",
        payment_amount: "",
        contact_name: "",
        contact_phone: "",
        default_school: {
          id: this.$store.state.school_id,
          name: this.$store.state.SchoolName
        },
        gender_choice: [{ value: 0, label: "男" }, { value: 1, label: "女" }],
        payment_choice: [
          { key: 0, value: "未交费" },
          { key: 1, value: "已缴费" }
        ]
      },
      // 编辑
      form2: {
        student_name: "",
        id: "",
        ID_number: "",
        birth_date: "",
        gender: "",
        registration_date: "",
        contact_name: "",
        contact_phone: "",
        payment_status: "",
        payment_amount: "",
        contact_name: "",
        contact_phone: "",
        gender_choice: [{ value: 0, label: "男" }, { value: 1, label: "女" }]
      }
    };
  },
  computed: {
    getSeasonId() {
      return this.$store.state.seasonId;
    }
  },
  watch: {
    // 利用watch方法检测seasonId变化,动态监听路由：
    getSeasonId: function(old, newd) {
      this.paidStuListInfo(this.$store.state.seasonId);
    }
  },

  activated: function() {
    console.log("activated");
    this.paidStuListInfo(this.$store.state.seasonId);
  },

  methods: {
    // 获取已付费学员信息
    paidStuListInfo(seasonId) {
      let that = this;
      that.$api
        .getPaidStuListInfo({
          params: {
            payment_status: 0,
            competition_season: parseInt(seasonId)
          }
        })
        .then(res => {
          // console.log(res, "....");
          that.tableData = res.data.results;
        });
    },

    // ===============================================

    // 编辑已缴费学员--获取行数据
    editClick(row) {
      // console.log(row, "12121212121");
      let that = this;
      that.form2.student_name = row.student_name;
      that.form2.ID_number = row.ID_number;
      that.form2.birth_date = row.birth_date;
      that.form2.gender = row.gender.value;
      that.form2.registration_date = row.registration_date;
      that.form2.payment_status = row.payment_status;
      that.form2.payment_amount = row.payment_amount;
      that.form2.id = row.id;
      that.form2.contact_name = row.contact_name;
      that.form2.contact_phone = row.contact_phone;
      // console.log(row, '333333333333333333333333');
    },

    // 编辑已缴费学员--提交行数据

    onSubmit(formName) {
      let that = this;
      that.form2.gender_choice.forEach(item => {
        if (item.label === that.form2.gender) {
          that.form2.gender = item.value;
        }
      });
      this.$refs[formName].validate(valid => {
        if (valid) {
          let postData = this.$qs.stringify({
            student_name: that.form2.student_name,
            ID_number: that.form2.ID_number,
            birth_date: that.form2.birth_date,
            gender: that.form2.gender,
            contact_name: that.form2.contact_name,
            contact_phone: that.form2.contact_phone
          });
          that.$api
            .stuListItemInfo(that.form2.id, postData)
            .then(res => {
              setTimeout(function() {
                that.centerDialogVisible1 = false;
              }, 1000);

              that.paidStuListInfo();

              that.$message({
                type: "success",
                message: "修改成功!"
              });
            })
            .catch(err => {
              console.log("err");
            });
        } else {
          setTimeout(function() {
            that.$message(
              {
                type: "warning",
                message: "填写信息不正确!"
              },
              2000
            );
          });
          return false;
        }
      });
    },

    // ================================================
    // 新增学员

    addStuSubmit(formName) {
      let that = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          let PostData = this.$qs.stringify({
            student_name: that.form.student_name,
            create_time: that.form.create_time,
            ID_number: that.form.ID_number,
            birth_date: that.form.birth_date,
            gender: that.form.gender.id,
            contact_name: that.form.contact_name,
            contact_phone: that.form.contact_phone,
            payment_amount: 0,
            payment_status: 0,
            school: that.$store.state.school_id,
            competition_season: that.$store.state.seasonId
          });
          that.$api
            .addStuInfo(PostData)
            .then(res => {
              that.centerDialogVisible = false;
              that.paidStuListInfo(this.$store.state.seasonId);
              setTimeout(function() {
                that.$message(
                  {
                    type: "success",
                    message: "新增成功!"
                  },
                  2000
                );
              });
            })
            .catch(err => {
              console.log("err");
            });
        } else {
          setTimeout(function() {
            that.$message(
              {
                type: "warning",
                message: "填写信息不正确!"
              },
              2000
            );
          });
          return false;
        }
      });
    },

    // =================================================

    // 删除学员
    deleteClick(obj) {
      // console.log(obj);
      let that = this;
      that.$api
        .DeletStuInfo(obj.id)
        .then(res => {
          console.log(res);
          if (res.status == 204) {
            that.paidStuListInfo(that.$store.state.seasonId);
            setTimeout(function() {
              that.$message({
                type: "success",
                message: "删除成功!"
              });
            }, 1000);
          }
        })
        .catch(err => {
          console.log("err");
        });
    },

    // =================================================

    // 搜索按钮

    searchBtn() {
      let that = this;
      that.$api
        .getPaidStuListInfo({
          params: {
            payment_status: 0,
            search: that.searchPerson
          }
        })
        .then(res => {
          that.tableData = res.data.results;
        })
        .catch(err => {
          console.log(err);
        });
    },

    // 缴费事件
    handleClick(row) {
      // console.log(row);
      let that = this;
      that.payStatus = true;
      that.payList.id = row.id;
    },
    // 缴费提交事件
    paySubmit() {
      let that = this;
      if (that.payList.pay) {
        that.$api
          .stuListItemInfo(that.payList.id, {
            payment_status: 1,
            payment_amount: that.payList.pay
          })
          .then(res => {
            that.paidStuListInfo(that.$store.state.seasonId);
            that.payStatus = false;
          });
      } else {
        that.$message({
          type: "warning",
          message: "请填写金额！"
        });
      }
    }
  }
};
</script>



<style scoped>
.add-btn {
  margin: 30px;
}

.search-btn {
  margin: 0 20px;
}

.edit-btn {
  margin: 0 10px;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100% !important;
}
</style>
